package com.huihuiyun.huihuiyun.server.controller.template.website;

import com.huihuiyun.huihuiyun.server.controller.template.website.authorize.TemplateWebsiteAuthorizeHandler;
import com.huihuiyun.huihuiyun.server.controller.template.website.viewobjects.HhyBookVO;
import com.huihuiyun.huihuiyun.server.domain.HhyBook;
import com.huihuiyun.huihuiyun.server.domain.HhyTemplate;
import com.huihuiyun.huihuiyun.server.service.HhyBookService;
import com.huihuiyun.huihuiyun.server.viewobject.ResultVO;
import com.huihuiyun.huihuiyun.server.viewobject.ResultVOUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.List;

@Path(Const.context)
@Component
@Slf4j
public class TemplateWebsiteBookController {
    @Autowired
    private TemplateWebsiteAuthorizeHandler templateWebsiteAuthorizeHandler;

    @Autowired
    private HhyBookService hhyBookService;

    @Context
    private HttpServletRequest httpServletRequest;

    @Context
    private HttpServletResponse httpServletResponse;

    /**
     * 是否有权限
     */
    private static boolean hasRight(HhyTemplate hhyTemplate, HhyBook hhyBook) {
        return true;
//        return StringUtils.equals(hhyBook.templateId, hhyTemplate.id);
    }

    @POST
    @Path("/book/add")
    @Produces(MediaType.APPLICATION_JSON)
    public ResultVO add(HhyBookVO hhyBookVO) {
        try {
            // 鉴权
            HhyTemplate hhyTemplate = templateWebsiteAuthorizeHandler.getHhyTemplate(httpServletRequest);
            if (hhyTemplate == null) {
                return templateWebsiteAuthorizeHandler.createNoAuthorizedResultVO(httpServletResponse);
            }
            // 业务
            HhyBook hhyBook = ResultVOUtil.convert(hhyBookVO, HhyBook.class);
            hhyBook.templateId = hhyTemplate.id;
            hhyBook = hhyBookService.put(hhyBook);
            hhyBookVO = ResultVOUtil.convert(hhyBook, HhyBookVO.class);
            return ResultVOUtil.success(hhyBookVO);
        } catch (Exception e) {
            log.error("", e);
            return ResultVOUtil.error("创建失败，请稍后重试");
        }
    }

    @POST
    @Path("/book/deleteOne")
    @Produces(MediaType.APPLICATION_JSON)
    public ResultVO deleteOne(HhyBookVO hhyBookVO) {
        try {
            // 鉴权
            HhyTemplate hhyTemplate = templateWebsiteAuthorizeHandler.getHhyTemplate(httpServletRequest);
            if (hhyTemplate == null) {
                return templateWebsiteAuthorizeHandler.createNoAuthorizedResultVO(httpServletResponse);
            }
            String id = hhyBookVO.id;
            HhyBook hhyBook = hhyBookService.findOne(id);
            if (!hasRight(hhyTemplate, hhyBook)) {
                return ResultVOUtil.error("没有权限");
            }
            // 业务
            hhyBookService.delete(id);
            return ResultVOUtil.success();
        } catch (Exception e) {
            log.error("", e);
            return ResultVOUtil.error("删除失败，请稍后重试");
        }
    }

    @POST
    @Path("/book/deleteAll")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public ResultVO deleteAll(List<HhyBookVO> hhyBookVOList) {
        try {
            // 鉴权
            HhyTemplate hhyTemplate = templateWebsiteAuthorizeHandler.getHhyTemplate(httpServletRequest);
            if (hhyTemplate == null) {
                return templateWebsiteAuthorizeHandler.createNoAuthorizedResultVO(httpServletResponse);
            }
            for (HhyBookVO hhyBookVO : hhyBookVOList) {
                String id = hhyBookVO.id;
                HhyBook hhyBook = hhyBookService.findOne(id);
                if (!hasRight(hhyTemplate, hhyBook)) {
                    return ResultVOUtil.error("没有权限");
                }
            }

            List<HhyBook> hhyBookList = ResultVOUtil.convertList(hhyBookVOList, HhyBook.class);
            // 业务
            for (HhyBook hhyBook : hhyBookList) {
                hhyBookService.delete(hhyBook.id);
            }
            return ResultVOUtil.success();
        } catch (Exception e) {
            log.error("", e);
            return ResultVOUtil.error("删除失败，请稍后重试");
        }
    }

    @POST
    @Path("/book/update")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public ResultVO updateBook(HhyBookVO hhyBookVO) {
        try {
            // 鉴权
            HhyTemplate hhyTemplate = templateWebsiteAuthorizeHandler.getHhyTemplate(httpServletRequest);
            if (hhyTemplate == null) {
                return templateWebsiteAuthorizeHandler.createNoAuthorizedResultVO(httpServletResponse);
            }
            String id = hhyBookVO.id;
            HhyBook hhyBook = hhyBookService.findOne(id);
            if (!hasRight(hhyTemplate, hhyBook)) {
                return ResultVOUtil.error("没有权限");
            }
            // 业务
            hhyBook = ResultVOUtil.copyProperties(hhyBookVO, hhyBook);
            hhyBook = hhyBookService.put(hhyBook);
            hhyBookVO = ResultVOUtil.convert(hhyBook, HhyBookVO.class);
            return ResultVOUtil.success(hhyBookVO);
        } catch (Exception e) {
            log.error("", e);
            return ResultVOUtil.error("更新失败，请稍后重试");
        }
    }

    @GET
    @Path("/book/findOne")
    @Produces(MediaType.APPLICATION_JSON)
    public ResultVO<HhyBookVO> findBook(@QueryParam("id") String id) {
        try {
            // 鉴权
            HhyTemplate hhyTemplate = templateWebsiteAuthorizeHandler.getHhyTemplate(httpServletRequest);
            if (hhyTemplate == null) {
                return templateWebsiteAuthorizeHandler.createNoAuthorizedResultVO(httpServletResponse);
            }
            HhyBook hhyBook = hhyBookService.findOne(id);
            if (!hasRight(hhyTemplate, hhyBook)) {
                return ResultVOUtil.error("没有权限");
            }
            // 业务
            HhyBookVO hhyBookVO = ResultVOUtil.convert(hhyBook, HhyBookVO.class);
            return ResultVOUtil.success(hhyBookVO);
        } catch (Exception e) {
            log.error("", e);
            return ResultVOUtil.error("获取数据失败，请稍后重试");
        }
    }

    @GET
    @Path("/book/findAll")
    @Produces(MediaType.APPLICATION_JSON)
    public ResultVO findAllBook(@QueryParam("currentPage") @DefaultValue("1") int currentPage, @QueryParam("pageSize") @DefaultValue("10") int pageSize) {
        try {
            // 鉴权
            HhyTemplate hhyTemplate = templateWebsiteAuthorizeHandler.getHhyTemplate(httpServletRequest);
            if (hhyTemplate == null) {
                return templateWebsiteAuthorizeHandler.createNoAuthorizedResultVO(httpServletResponse);
            }
            // 业务
            String templateId = hhyTemplate.id;
            currentPage = ResultVOUtil.getCurrentPageFromAntDesignProQueryParam(currentPage);
            Page<HhyBook> bookPage = hhyBookService.findAllByTemplateId(templateId, currentPage, pageSize);
            Object data = ResultVOUtil.convertJpaPage2AntDesignTableData(bookPage, HhyBookVO.class);
            return ResultVOUtil.success(data);
        } catch (Exception e) {
            log.error("", e);
            return ResultVOUtil.error("获取数据失败，请稍后重试");
        }
    }

}
